<template>
  <div class="dashboard-container">
    <div class="app-container">
      <template>
        <div class="dashboard-container">
          <div class="app-container">
            <el-card>
              <el-tabs>
                <!-- 放置页签 -->
                <el-tab-pane label="角色管理">
                  <!-- 新增角色按钮 -->
                  <el-row style="height:60px">
                    <el-button
                      icon="el-icon-plus"
                      size="small"
                      type="primary"
                      @click="showDialog=true"
                    >新增角色</el-button>
                  </el-row>
                  <!-- 表格 -->
                  <el-table border="" :data="list">
                    <el-table-column align="center" type="index" label="序号" width="120" />
                    <el-table-column align="center" prop="name" label="角色名称" width="240" />
                    <el-table-column align="center" prop="description" label="描述" />
                    <el-table-column label="操作">
                      <!-- 作用域插槽 -->
                      <template slot-scope="{row}">
                        <el-button size="small" type="success">分配权限</el-button>
                        <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                        <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                      </template>

                    </el-table-column>
                  </el-table>
                  <!-- 分页组件 -->
                  <el-row type="flex" justify="center" align="middle" style="height: 60px">
                    <!-- 分页组件 -->
                    <el-pagination
                      background
                      :current-page="page.page"
                      :page-size="page.pagesize"
                      :total="page.total"
                      :page-sizes="[2,4,6]"
                      layout="prev,pager,next,sizes"
                      @current-change="changePage"
                      @size-change="changeSize"
                    />
                  </el-row>
                </el-tab-pane>
                <el-tab-pane label="公司信息">
                  <el-alert
                    title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
                    type="info"
                    show-icon
                    :closable="false"
                  />
                  <el-form label-width="120px" style="margin-top:50px">
                    <el-form-item label="公司名称">
                      <el-input v-model="formData.name" disabled style="width:400px" />
                    </el-form-item>
                    <el-form-item label="公司地址">
                      <el-input v-model="formData.companyAddress" disabled style="width:400px" />
                    </el-form-item>
                    <el-form-item label="邮箱">
                      <el-input v-model="formData.mailbox" disabled style="width:400px" />
                    </el-form-item>
                    <el-form-item label="备注">
                      <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px" />
                    </el-form-item>
                  </el-form>
                </el-tab-pane>

              </el-tabs>
            </el-card>
          </div>
        </div>
      </template>
    </div>
    <!-- 放置一个弹层组件 -->
    <el-dialog
      :title="showTitle"
      :visible="showDialog"
      :close-on-click-modal="false"
      @close="btnCancel"
    >
      <el-form ref="roleForm" label-width="120px" :model="roleForm" :rules="rules">
        <el-form-item prop="name" label="角色名称">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 放置footer插槽 -->
      <el-row type="flex" justify="center">
        <el-col :span="8">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole } from '@/api/settings'
import { mapGetters } from 'vuex'
export default {
  name: 'Settings',
  data() {
    return {
      list: [], // 承接数组
      page: {
        page: 1,
        pagesize: 2, // 一页放几个数据
        total: 0 // 记录总数
      },
      formData: {}, // 表单信息变量
      showDialog: false, // 控制弹层显示
      roleForm: {},
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    showTitle() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      // console.log(res)
      this.page.total = total
      this.list = rows
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId) // 赋值给formData
    },
    changePage(newPage) {
      // console.log(newPage)
      // newPage是当前点击的页码
      this.page.page = newPage // 将当前页码赋值给当前的最新页码
      this.getRoleList()
    },
    // 每页数量修改事件
    changeSize(val) {
      // val是每页最新的条数
      this.page.pagesize = val
      this.getRoleList()
    },
    // 删除
    async deleteRole(id) {
      try {
        // 1.确认框
        await this.$confirm('确认删除吗？')
        // 2.删除请求发送
        await deleteRole(id)
        // 3.操作提示
        this.$message.success('删除成功')
        if (this.list.length === 1 && this.page.page !== 1) {
          this.page.page--
        }
        // 4.拉取列表
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    async editRole(id) {
      this.roleForm = await getRoleDetail(id)
      this.showDialog = true
    },
    // 提交事件
    async btnOK() {
      try {
        await this.$refs.roleForm.validate()
        // 只有校验通过的情况下 才会执行await下方内容
        // roleForm 这个对象有id就是编辑，没有就是新增
        if (this.roleForm.id) {
          // 有id为编辑
          await updateRole(this.roleForm)
        } else {
          // 没有id为新增
          await addRole(this.roleForm)
        }
        // 重新拉取数据
        this.getRoleList()
        // 成功提示
        this.$message.success('操作成功')
        // 关闭弹层
        this.showDialog = false
      } catch (error) {
        console.log(error)
      }
    },
    // 取消事件
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      // 移除校验
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    }
  }

}
</script>

<style>

</style>
